package cc.dd.marketing.rule;

import cc.dd.marketing.rule.base.Rule;
import cc.dd.marketing.rule.base.RuleChecker;
import cc.dd.marketing.system.helper.BeanHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * check
 *
 * @author dongsufeng
 * @date 2022/9/9 2:51 PM
 */
public abstract class CheckerEngine<T extends Rule, C extends RuleChecker> {

    @Resource
    protected BeanHelper beanHelper;

    /**
     * 获取对应的检查类
     *
     * @param rule
     * @return
     */
    protected C getChecker(T rule) {
        String clazzName = rule.getClass().getSimpleName().concat("Checker");
        return (C) this.beanHelper.getBean(StringUtils.uncapitalize(clazzName));
    }

    /**
     * 内部执行类
     *
     * @param <T>
     */
    public abstract class CheckerProcess<T> {
        public CheckerProcess() {
        }

        /**
         * 执行方法
         *
         * @param checkList
         * @return
         */
        public Boolean process(List<T> checkList) {
            if (CollectionUtils.isEmpty(checkList)) {
                return true;
            }
            checkList.stream().forEach(item -> _process(item));
            return true;
        }


        /**
         * @param checker
         * @return
         */
        public abstract Boolean _process(T checker);
    }
}
